package com.zscat.mallplus.search.controller;

import com.zscat.mallplus.pms.vo.HomeProductInfo;
import com.zscat.mallplus.search.domain.EsProduct;
import com.zscat.mallplus.search.domain.ProductRelatedInfo;
import com.zscat.mallplus.search.domain.SearchPageInfo;
import com.zscat.mallplus.search.service.EsProductService;
import com.zscat.mallplus.utils.CommonResult;
import com.zscat.mallplus.vo.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 搜索商品管理Controller
 * Created by mallplus on 2018/6/19.
 */
@RestController
@RequestMapping("/api/productSearch")
public class ProductSearchController {

    @Autowired
    private EsProductService esProductService;

    /**
     * 导入所有数据库中商品到ES
     *
     * @return
     */
    @RequestMapping(value = "/importAll", method = RequestMethod.POST)
    @ResponseBody
    public Object importAllList() {
        int count = esProductService.importAll();
        return new CommonResult().success(count);
    }

    /**
     * 搜索页面
     *
     * @return
     */
    @RequestMapping(value = "/search/searchPage", method = RequestMethod.GET)
    @ResponseBody
    public Object searchPage() {
        SearchPageInfo searchPageInfo = new SearchPageInfo();
        //女小串冷冻素圈
        List<String> interestes = esProductService.getInterestesList();
//        interestes.add("手机");
//        interestes.add("男装");
//        interestes.add("女装");
//        interestes.add("周大生项链女");
//        interestes.add("戒指");
//        interestes.add("烧烤");
//        interestes.add("自行车");
//        interestes.add("滑板车");
//        interestes.add("扇子");
//        interestes.add("猫粮");
//        interestes.add("t恤");
//        interestes.add("长袖");
//        interestes.add("食材");
//        interestes.add("华为");
//        interestes.add("小米");
//        interestes.add("冷冻素圈");
//        interestes.add("苹果");
//        interestes.add("水果");
//        interestes.add("内衣");
//        interestes.add("茶叶");
        searchPageInfo.setInterestes(interestes);
        return new CommonResult().success(searchPageInfo);
    }

    /**
     * 搜索关键字联想
     *
     * @param keyword
     * @return
     */
    @RequestMapping(value = "/search/association", method = RequestMethod.GET)
    @ResponseBody
    public Object searchAssociation(@RequestParam(required = false) String keyword) {
        Page<EsProduct> esProductPage = esProductService.search(keyword, 0, 20);
        Set<String> keywords = new HashSet<>();
        for (EsProduct esProduct : esProductPage.getContent()) {
            keywords.add(esProduct.getName());
        }
        return new CommonResult().success(keywords);
    }

    /**
     * 输入关键字搜索
     */
    @RequestMapping(value = "/search/simple", method = RequestMethod.GET)
    @ResponseBody
    public Object search(@RequestParam(required = false) String keyword,
                         @RequestParam(required = false, defaultValue = "1") Integer pageIndex,
                         @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        pageIndex = pageIndex - 1;
        Page<EsProduct> esProductPage = esProductService.search(keyword, pageIndex, pageSize);
        PageInfo<EsProduct> page = new PageInfo<>(pageIndex + 1, pageSize, esProductPage.getContent());
        return new CommonResult().success(page);
    }

    /**
     * 输入关键字综合搜索、筛选、排序
     * 排序字段:0->按相关度；1->按新品；2->按销量；3->价格从低到高；4->价格从高到低
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    @ResponseBody
    public Object search(@RequestParam(required = false) String keyword,
                         @RequestParam(required = false) Long brandId,
                         @RequestParam(required = false) Long areaId,
                         @RequestParam(required = false) Long productCategoryId,
                         @RequestParam(required = false, defaultValue = "1") Integer pageIndex,
                         @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                         @RequestParam(required = false, defaultValue = "0") Integer sort) {
        pageIndex = pageIndex - 1;
        Page<EsProduct> esProductPage = esProductService.searchV1(keyword, brandId, areaId, pageIndex, pageSize, sort, productCategoryId);
        PageInfo<EsProduct> page = new PageInfo<>(pageIndex + 1, pageSize, esProductPage.getContent());
        return new CommonResult().success(page);
    }

    /**
     * 获取搜索的相关品牌、分类及筛选属性
     *
     * @param keyword
     * @return
     */
    @RequestMapping(value = "/search/relate", method = RequestMethod.GET)
    @ResponseBody
    public Object searchRelatedInfo(@RequestParam(required = false) String keyword, Long productCategoryId) {
//        EsProductRelatedInfo productRelatedInfo = esProductService.searchRelatedInfoV1(keyword);
//        return new CommonResult().success(productRelatedInfo);
        ProductRelatedInfo productRelatedInfo = esProductService.searchRelatedInfoV2(keyword, productCategoryId);
        return new CommonResult().success(productRelatedInfo);
    }

    /**
     * 获取搜索的相关品牌、分类及筛选属性
     *
     * @param keyword
     * @return
     */
    @RequestMapping(value = "/search/relateV1", method = RequestMethod.GET)
    @ResponseBody
    public Object searchRelatedInfoV1(@RequestParam(required = false) String keyword, Long productCategoryId) {
        ProductRelatedInfo productRelatedInfo = esProductService.searchRelatedInfoV2(keyword, productCategoryId);
        return new CommonResult().success(productRelatedInfo);
    }

//    @ApiOperation(value = "根据商品id推荐商品")

    /**
     * 根据商品id推荐商品
     *
     * @param id
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/recommend", method = RequestMethod.GET)
    @ResponseBody
    public Object recommend(Long id,
                            @RequestParam(required = false, defaultValue = "0") Integer pageNum,
                            @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        Page<EsProduct> esProductPage = esProductService.recommend(id, pageNum, pageSize);
        List<HomeProductInfo> list = new ArrayList<>();
        for (EsProduct esProduct : esProductPage.getContent()) {
            HomeProductInfo homeProductInfo = new HomeProductInfo();
            homeProductInfo.setPrice(Float.parseFloat(esProduct.getPrice().toString()));
            homeProductInfo.setProductId(esProduct.getId().toString());
            homeProductInfo.setProductImage(esProduct.getPic());
            homeProductInfo.setProductName(esProduct.getName());
            homeProductInfo.setSale(esProduct.getSale());

            list.add(homeProductInfo);
        }
        return new CommonResult().success(list);
    }

}
